﻿#region File and License Information
/*
<File>
	<License Type="BSD">
		Copyright © 2009 - 2012, Outcoder. All rights reserved.
	
		This file is part of Calcium (http://calciumsdk.net).

		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are met:
			* Redistributions of source code must retain the above copyright
			  notice, this list of conditions and the following disclaimer.
			* Redistributions in binary form must reproduce the above copyright
			  notice, this list of conditions and the following disclaimer in the
			  documentation and/or other materials provided with the distribution.
			* Neither the name of the <organization> nor the
			  names of its contributors may be used to endorse or promote products
			  derived from this software without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
		DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
		DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
		(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
		ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	</License>
	<Owner Name="Daniel Vaughan" Email="danielvaughan@outcoder.com" />
	<CreationDate>2009-04-15 21:44:59Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Controls;
using Outcoder.IO;

namespace Outcoder.IO
{
	/// <summary>
	/// Used to invoke a file operation that works upon a file
	/// with the specified <code>fileInfo</code>.
	/// </summary>
	public delegate void FileOperationHandler(System.IO.FileInfo fileInfo);
	public delegate void MultiFileOperationHandler(IEnumerable<System.IO.FileInfo> fileInfos);
	/* TODO: [DV] Comment. */
	public delegate void SaveFileOperationHandler(string fileName, Func<System.IO.Stream> getStream);
}

namespace Outcoder.Services
{
	public class FileSelectionArgs
	{
		public FileSelectionArgs(IList<FileSelectionFilter> filters, int? dialogSelectedIndex)
		{
			ArgumentValidator.AssertNotNull(filters, "filters");
			this.filters = new List<FileSelectionFilter>(filters);
			if (dialogSelectedIndex.HasValue && dialogSelectedIndex + 1 > filters.Count)
			{
				throw new ArgumentOutOfRangeException("dialogSelectedIndex");
			}
			this.filterIndex = dialogSelectedIndex;
		}

		public FileSelectionArgs()
		{
			/* TODO: Make localizable resource. */
			filters = new List<FileSelectionFilter> { new FileSelectionFilter("All Files", "*.*") };
		}

		public int FilterCount
		{
			get
			{
				return filters.Count;
			}
		}

		readonly int? filterIndex;

		public int? FilterIndex
		{
			get
			{
				return filterIndex;
			}
		}

		internal string FilterString
		{
			get
			{
				StringBuilder sb = new StringBuilder();
				int filterCount = filters.Count;
				foreach (var filter in filters)
				{
					sb.Append(filter);
					if (filterCount > 1)
					{
						sb.Append('|');
					}
				}
				return sb.ToString();
			}
		}

		readonly List<FileSelectionFilter> filters;

		public IList<FileSelectionFilter> Filters
		{
			get
			{
				/* TODO: Make localizable resource. */
				return filters;
			}
		}
	}

	public class OpenFileSelectionArgs : FileSelectionArgs
	{
		public OpenFileSelectionArgs(IList<FileSelectionFilter> filters, int? dialogSelectedIndex) 
			: base(filters, dialogSelectedIndex)
		{
			/* Intentionally left blank. */
		}

		public OpenFileSelectionArgs()
		{
			/* Intentionally left blank. */
		}

		public bool MultiSelect { get; set; }

		public void SetDialogProperties(OpenFileDialog fileDialog)
		{
			ArgumentValidator.AssertNotNull(fileDialog, "fileDialog");
			fileDialog.Filter = FilterString;
			if (FilterIndex.HasValue)
			{
				fileDialog.FilterIndex = FilterIndex.Value;
			}
			fileDialog.Multiselect = MultiSelect;
		}
	}

	public class SaveFileSelectionArgs : FileSelectionArgs
	{
		public SaveFileSelectionArgs()
		{
			/* Intentionally left blank. */
		}

		public SaveFileSelectionArgs(IList<FileSelectionFilter> filters, int? dialogSelectedIndex) : base(filters, dialogSelectedIndex)
		{
			/* Intentionally left blank. */
		}

		public string FileExtension { get; set; }

		public void Populate(SaveFileDialog fileDialog)
		{
			ArgumentValidator.AssertNotNull(fileDialog, "fileDialog");
			fileDialog.Filter = FilterString;
			if (FilterIndex.HasValue)
			{
				fileDialog.FilterIndex = FilterIndex.Value;
			}
			fileDialog.DefaultExt = FileExtension;
		}
	}

	public class FileSelectionFilter
	{
		public string Name { get; private set; }
		public string FileExtension { get; private set; }

		public FileSelectionFilter(string name, string fileExtension)
		{
			Name = name;
			FileExtension = fileExtension;
		}

		public override string ToString()
		{
			return string.Format("{0}|{1}", Name, FileExtension);
		}
	}



	/// <summary>
	/// Allows for the loading and saving of file content
	/// using delegates for invocation.
	/// </summary>
	public interface IFileService
	{
		FileOperationResult SaveAs(SaveFileOperationHandler operationHandler, FileErrorAction fileErrorAction,
			SaveFileSelectionArgs fileSelectionArgs);

		FileOperationResult Save(SaveFileOperationHandler operationHandler, FileErrorAction fileErrorAction, 
			Func<Stream> getStreamFunc);

		FileOperationResult Open(FileOperationHandler operationHandler, FileErrorAction fileErrorAction, OpenFileSelectionArgs fileSelectionArgs);
		FileOperationResult Open(MultiFileOperationHandler operationHandler, FileErrorAction fileErrorAction, OpenFileSelectionArgs fileSelectionArgs);

	}
}
